﻿using BaseDll;
using CommonTools;
using FinalTest_Algonrithm;
using HY_UPHMgr;
using MotionIoLib;
using Newtonsoft.Json;
using OtherDevice;
using StationCommunication;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using UPH;
using UserCtrl;
using UserData;
using TestResult = DaHuaMes.TestResult;

namespace StationDemo
{

    public class Param_Load
    {
        public ParamSet 出图图像宽 = new ParamSet()
        {
            _enuValType = ParamSetUnit.intUnit,
            _strParamUnit = "pix",
            _strParamValMax = 600000,
            _strParamValMin = 0,
            _strParamVal = 0,
            _ParamRight = UserRight.Operator,
            _ParamClass = "出图",
            b_IsStartCheck = true,
        };
        public ParamSet 出图图像高 = new ParamSet()
        {
            _enuValType = ParamSetUnit.intUnit,
            _strParamUnit = "pix",
            _strParamValMax = 600000,
            _strParamValMin = 0,
            _strParamVal = 0,
            _ParamRight = UserRight.Operator,
            _ParamClass = "出图",
            b_IsStartCheck = true,
        };

        public ParamSet 屏蔽SocketA = new ParamSet()
        {
            _enuValType = ParamSetUnit.boolUnit,
            _strParamUnit = "屏蔽SocketA",
            _strParamValMax = true,
            _strParamValMin = false,
            _strParamVal = false,
            _ParamRight = UserRight.Operator,
            _ParamClass = "上料",
            b_IsStartCheck = true,
        };
        public ParamSet 屏蔽SocketB = new ParamSet()
        {
            _enuValType = ParamSetUnit.boolUnit,
            _strParamUnit = "屏蔽SocketB",
            _strParamValMax = true,
            _strParamValMin = false,
            _strParamVal = false,
            _ParamRight = UserRight.Operator,
            _ParamClass = "上料",
            b_IsStartCheck = true,
        };


        public ParamSet 屏蔽扫码 = new ParamSet()
        {
            _enuValType = ParamSetUnit.boolUnit,
            _strParamUnit = "屏蔽扫码",
            _strParamValMax = true,
            _strParamValMin = false,
            _strParamVal = false,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };
        public ParamSet 屏蔽电压电流测试 = new ParamSet()
        {
            _enuValType = ParamSetUnit.boolUnit,
            _strParamUnit = "屏蔽电压电流测试",
            _strParamValMax = true,
            _strParamValMin = false,
            _strParamVal = false,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };
        public ParamSet 键盘模式 = new ParamSet()
        {
            _enuValType = ParamSetUnit.boolUnit,
            _strParamUnit = "键盘模式",
            _strParamValMax = true,
            _strParamValMin = false,
            _strParamVal = false,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };
        public ParamSet 扫码超时时间 = new ParamSet()
        {
            _enuValType = ParamSetUnit.intUnit,
            _strParamUnit = "扫码超时时间",
            _strParamValMax = 600000,
            _strParamValMin = 0,
            _strParamVal = 60000,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };
        public ParamSet 是否为手动治具 = new ParamSet()
        {
            _enuValType = ParamSetUnit.boolUnit,
            _strParamUnit = "是否为手动治具",
            _strParamValMax = true,
            _strParamValMin = false,
            _strParamVal = false,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };
    }
    public class Point_Load
    {
    }
    public class Config_Load
    {

        [Category("出图参数"), DisplayName("1.工位A采集卡SN"), Description("工位A采集卡SN")]
        public string str_StationACapBoxSN { set; get; } = "";

        [Category("出图参数"), DisplayName("2.工位B采集卡SN"), Description("工位B采集卡SN")]
        public string str_StationBCapBoxSN { set; get; } = "";

        [Category("工站参数"), DisplayName("1.左压合气缸"), Description("左压合气缸")]
        public string str_左压合气缸 { set; get; } = "左压合气缸";
        [Category("工站参数"), DisplayName("2.左顶升气缸"), Description("左顶升气缸")]
        public string str_左顶升气缸 { set; get; } = "左顶升气缸";
        [Category("工站参数"), DisplayName("1.右压合气缸"), Description("右压合气缸")]
        public string str_右压合气缸 { set; get; } = "右压合气缸";
        [Category("工站参数"), DisplayName("2.右顶升气缸"), Description("右顶升气缸")]
        public string str_右顶升气缸 { set; get; } = "右顶升气缸";

        [Category("工站参数"), DisplayName("3.扫码枪"), Description("扫码枪")]
        public string str_扫码枪 { set; get; } = "扫码枪";
    }

    public class Station_Load : CommonTools.StationbaseEx<Param_Load, Point_Load, Config_Load>
    {
        #region 变量
        Station_Table station_table = null;
        StationInfor loadStation = null;

        int nUPHIndex = 2;



        ClylinerBase Clyliner_左压合气缸
        {
            get
            {
                return DeviceMgr.GetInstance().Function_获取气缸(station_Config.str_左压合气缸);
            }
        }
        ClylinerBase Clyliner_左顶升气缸
        {
            get
            {
                return DeviceMgr.GetInstance().Function_获取气缸(station_Config.str_左顶升气缸);
            }
        }
        ClylinerBase Clyliner_右压合气缸
        {
            get
            {
                return DeviceMgr.GetInstance().Function_获取气缸(station_Config.str_右压合气缸);
            }
        }
        ClylinerBase Clyliner_右顶升气缸
        {
            get
            {
                return DeviceMgr.GetInstance().Function_获取气缸(station_Config.str_右顶升气缸);
            }
        }

        SNScan_Base 扫码枪
        {
            get
            {
                return DeviceMgr.GetInstance().Function_获取串口(station_Config.str_扫码枪) as Scan_NLSFM430;
            }
        }
        public Station_Load(CommonTools.Stationbase station, Param_Load param, Point_Load point, Config_Load config) : base(station, param, point, config)
        {
            ModuleCaptureMgr.Instance.GetIndex = (index) =>
            {
                switch (index)
                {
                    case 0:
                        return station_Config.str_StationACapBoxSN;
                    case 1:
                        return station_Config.str_StationBCapBoxSN;
                }
                return station_Config.str_StationACapBoxSN;
            };
        }


        public enum StationStep
        {
            [Description("0.初始化步骤")]
            Step_Init = 100,
            [Description("1.等待转盘到位")]
            Step_等待转盘到位,
            [Description("2.检测当前工位产品")]
            Step_CheckProduct,
            [Description("3.扫码")]
            Step_扫码,
            [Description("4.启动")]
            Step_启动,
            [Description("5.测试")]
            Step_测试,
            [Description("6.通知转盘")]
            Step_通知转盘,
            [Description("7.下料")]
            Step_下料,
        }
        #endregion

        #region 主流程
        protected override void StationWork(int step)
        {
            try
            {
                Thread.Sleep(50);
                switch (step)
                {
                    case (int)StationStep.Step_Init:
                        DelCurrentStep();
                        PushMultStep((int)StepInitRun());
                        break;
                    case (int)StationStep.Step_等待转盘到位:
                        DelCurrentStep();
                        PushMultStep((int)Step_等待转盘到位());
                        break;
                    case (int)StationStep.Step_CheckProduct:
                        DelCurrentStep();
                        PushMultStep((int)Step_CheckProduct());
                        break;
                    case (int)StationStep.Step_扫码:
                        DelCurrentStep();
                        PushMultStep((int)Step_扫码());
                        break;
                    case (int)StationStep.Step_启动:
                        DelCurrentStep();
                        PushMultStep((int)Step_启动());
                        break;
                    case (int)StationStep.Step_测试:
                        DelCurrentStep();
                        PushMultStep((int)Step_测试());
                        break;
                    case (int)StationStep.Step_通知转盘:
                        DelCurrentStep();
                        PushMultStep((int)Step_通知());
                        break;
                    case (int)StationStep.Step_下料:
                        DelCurrentStep();
                        PushMultStep((int)Step下料());
                        break;
                }
            }
            catch (Exception ex)
            {
                LogHelper.OnTest_ShowLog(Name, $"{Name} 发生错误:{ex.StackTrace},{ex.Message}", LogType.Err);
                StationMgr.GetInstance().Stop();
            }
        }

        protected override bool InitStation()
        {
            if (station_Param.屏蔽SocketA._strParamVal && station_Param.屏蔽SocketB._strParamVal)
            {
                throw new Exception("不可同时禁用两个治具");
            }
            if (Param_System.Instance.外接标定程序._strParamVal)
            {
                LoadSeverMgr.Instance.Function_通知标定开始测试(LoadSeverMgr.Instance.server1);
            }
            ClearAllStep();
            PushMultStep((int)StationStep.Step_Init);
            this.station_table = (Station_Table)StationMgr.GetInstance().GetStation(Station.转盘站.ToString());
            this.loadStation = new StationInfor(SOCKET.SOCKETA);
            return true;
        }
        #endregion

        #region 方法

        public StationStep StepInitRun(bool bmanual = false)
        {
            UPHTimeMgr.Instance.AddTimeBeatType(Name);
            LogHelper.OnTest_ShowLog(Name, $"上料站，等待转盘旋转到位", LogType.Warn);
            return StationStep.Step_等待转盘到位;
        }

        StationStep Step_等待转盘到位(bool bmanual = false)
        {
            UPHTimeMgr.Instance.Star(nUPHIndex, MergeModel.StandAlone);
            if (station_table.TableStatus == TABLESTATUS.stop || station_table.TableStatus == TABLESTATUS.ready_clibra)//等待转盘到位，测试开始信号
            {
                LogHelper.OnTest_ShowLog(Name, $"上料站，转盘旋转到位，开始流程", LogType.Warn);
                UPHTimeMgr.Instance.AddData(nUPHIndex, Name, $"转盘旋转到位");
                return StationStep.Step_CheckProduct;
            }
            return StationStep.Step_等待转盘到位;
        }

        StationStep Step_CheckProduct(bool bmanual = false)
        {
            string productJson = station_table.Function_GetProduct(Station.上料站);
            this.loadStation = JsonConvert.DeserializeObject<StationInfor>(productJson);

            if (loadStation.socket == SOCKET.SOCKETA && station_Param.屏蔽SocketA._strParamVal)
            {
                LogHelper.OnTest_ShowLog(Name, $"上料站：{loadStation.socket.ToString()}被禁用", LogType.Warn);
                return StationStep.Step_通知转盘;
            }

            if (loadStation.socket == SOCKET.SOCKETB && station_Param.屏蔽SocketB._strParamVal)
            {
                LogHelper.OnTest_ShowLog(Name, $"上料站：{loadStation.socket.ToString()}被禁用", LogType.Warn);
                return StationStep.Step_通知转盘;
            }

            if (this.loadStation.SOCKETSTATUS == SOCKETSTATUS.NoProduct)
            {
                LogHelper.OnTest_ShowLog(Name, $"上料站，无产品，开始扫码");
                this.loadStation = new StationInfor(loadStation.socket);
                return StationStep.Step_扫码;
            }
            else
            {
                LogHelper.OnTest_ShowLog(Name, $"上料站，有产品，开始下料", LogType.Warn);
                return StationStep.Step_下料;
            }
        }

        public StationStep Step_扫码(bool bmanual = false)
        {
            if (UserConfig.b_退料)
            {
                LogHelper.OnTest_ShowLog(Name, $"上料站，收到退料请求", LogType.Warn);
                UserConfig.b_退料 = false;
                return StationStep.Step_通知转盘;
            }

            StationStateHelper.OnTest_ShowStep(Name, "请扫码", LogType.Warn);
            LogHelper.OnTest_ShowLog(Name, $"请扫码", LogType.Warn);
            string SN = "";
            if (!Function_扫码(ref SN))
            {
                return StationStep.Step_扫码;
            }

            LogHelper.OnTest_ShowLog(Name, $"扫码成功，条码为：{SN}", LogType.Warn);

            UPHTimeMgr.Instance.AddData(nUPHIndex, Name, $"扫码完成");
            SNHelper.OnTest_ShowSN("SN", SN);
            if (Param_System.Instance.是否开启Mes._strParamVal)
            {
                //进站
                bool res = Function_进站(SN, out string msg);
                if (res)
                {
                    LogHelper.OnTest_ShowLog("MES", $"条码【{SN}】进站成功", LogType.Warn);
                }
                else
                {
                    WaranResult result = AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.MES进站失败, new string[] { "重试" }, new string[] { $"条码【{SN}】进站失败原因：{msg}，请重新扫码" });
                    if (result == WaranResult.Custom1)
                    {
                        LogHelper.OnTest_ShowLog("MES", $"条码【{SN}】进站失败,失败原因：{msg}", LogType.Warn);
                        LogHelper.OnTest_ShowLog("MES", "请重新扫码进站：", LogType.Warn);
                        Thread.Sleep(200);
                        // Form_Auto.ShowSN("");
                        return StationStep.Step_扫码;
                    }



                };

                // UserTest.Result[index].str_外壳SN.Value = SN;
                UPHTimeMgr.Instance.AddData(nUPHIndex, Name, $"进站完成");
            }

            loadStation.productData.sn.Value = SN;
            loadStation.SOCKETSTATUS = SOCKETSTATUS.HaveProductCantTest;

            return StationStep.Step_启动;
        }

        public StationStep Step_启动(VisionControl visionControl = null, bool bmanual = false)
        {
            StationStateHelper.OnTest_ShowStep(Name, "请上料，并按下双启动", LogType.Warn);

            LogHelper.OnTest_ShowLog(Name, "等待启动信号");
            if (sys.g_AppMode == AppMode.AirRun)
            {
                loadStation.SOCKETSTATUS = SOCKETSTATUS.HaveProductCanTest;
                for (int i = 0; i < 3; i++)
                {
                    IOMgr.GetInstace().WriteIoBit(IO_OUT.双启动灯A.ToString(), true);
                    IOMgr.GetInstace().WriteIoBit(IO_OUT.双启动灯B.ToString(), true);
                    Thread.Sleep(200);
                    IOMgr.GetInstace().WriteIoBit(IO_OUT.双启动灯A.ToString(), false);
                    IOMgr.GetInstace().WriteIoBit(IO_OUT.双启动灯B.ToString(), false);
                    Thread.Sleep(200);
                }

                Thread.Sleep(200);

                return StationStep.Step_测试;

            }
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            while (true)
            {
                if (UserConfig.b_退料)
                {
                    LogHelper.OnTest_ShowLog(Name, $"上料站，收到退料请求", LogType.Warn);
                    UserConfig.b_退料 = false;
                    return StationStep.Step_通知转盘;
                }
                if (/*IOMgr.GetInstace().Function_ReCheckIO(IO_IN.双启动信号A.ToString(), true,100) &&*/ IOMgr.GetInstace().Function_ReCheckIO(IO_IN.双启动信号B.ToString(), true, 100))
                {
                    LogHelper.OnTest_ShowLog(Name, "双启动按下");
                    IO_OUT.双启动灯A.Set(false);
                    IO_OUT.双启动灯B.Set(false);
                    break;
                }
                Thread.Sleep(1);
                if (stopwatch.ElapsedMilliseconds > 200)
                {
                    stopwatch.Restart();
                    IO_OUT.双启动灯A.Set(!IO_OUT.双启动灯A.Get());
                    IO_OUT.双启动灯B.Set(!IO_OUT.双启动灯B.Get());
                }
            }
            loadStation.SOCKETSTATUS = SOCKETSTATUS.HaveProductCanTest;
            IOMgr.GetInstace().WriteIoBit(IO_OUT.指示灯绿.ToString(), false);
            IOMgr.GetInstace().WriteIoBit(IO_OUT.指示灯红.ToString(), false);
            GlobalVariable.启用安全光栅 = true;
            return StationStep.Step_测试;
        }

        public StationStep Step下料(bool bmanual = false)
        {
            string v = station_table.Function_GetProduct(Station.上料站);
            loadStation = JsonConvert.DeserializeObject<StationInfor>(v);
            double ct = (DateTime.Now - loadStation.startTime).TotalSeconds;
            bool isOK = loadStation.productData.result.Value;
            LogHelper.OnTest_ShowLog(Name, $"测试完毕，CT:{ct},结果{isOK}", isOK ? LogType.Warn : LogType.Err);
            if (isOK)
            {
                TestTimeMgr.GetInstance().Function_AddTestTime("数据统计", ct, true);
            }
            else
            {
                NGCountMgr.GetInstance().AddNGCount(Station.标定站.ToString(), ClibraNGType.标定.ToString(), 1);
                TestTimeMgr.GetInstance().Function_AddTestTime("数据统计", ct, false);
            }
            this.loadStation = new StationInfor(loadStation.socket);
            return StationStep.Step_扫码;
        }

        public StationStep Step_测试(bool bmanual = false)
        {
            StationStateHelper.OnTest_ShowStep(Name, "开始点亮测试", LogType.Warn);
            LogHelper.OnTest_ShowLog(Name, "开始点亮测试");

            if (Param_System.Instance.外接标定程序._strParamVal)
            {
                double vv = 0.0;
                double cc = 0.0;
                bool ecv = LoadSeverMgr.Instance.Function_点亮(LoadSeverMgr.Instance.server1, loadStation.productData.sn.Value, ref vv, ref cc);
                if (!ecv)
                {
                    ecv = LoadSeverMgr.Instance.Function_点亮(LoadSeverMgr.Instance.server1, loadStation.productData.sn.Value, ref vv, ref cc);
                    if (!ecv)
                    {
                        LogHelper.OnTest_ShowLog(Name, $"点亮失败\n", LogType.Err);
                        NGCountMgr.GetInstance().AddNGCount(loadStation.socket == SOCKET.SOCKETA ? Station.治具A站.ToString() : Station.治具B站.ToString(), SocketNGType.点亮.ToString(), 1);
                        TestTimeMgr.GetInstance().Function_AddTestTime("数据统计", 0, false);
                        return StationStep.Step_扫码;
                    }
                }
                LoadSeverMgr.Instance.Function_清空标定数据(LoadSeverMgr.Instance.server1);
                return StationStep.Step_通知转盘;
            }

            string capBoxSN = loadStation.socket == SOCKET.SOCKETA ? station_Config.str_StationACapBoxSN : station_Config.str_StationBCapBoxSN;
            LogHelper.OnTest_ShowLog(Name, $"socket:[{loadStation.socket.ToString()}],capBoxSN:[{capBoxSN}]");
            bool v = Function_CapImg(capBoxSN, false, out string msg);
            if (!v)
            {
                msg = "";
                v = Function_CapImg(capBoxSN, false, out msg);
                if (!v)
                {
                    LogHelper.OnTest_ShowLog(Name, $"点亮失败\n{msg}", LogType.Err);
                    NGCountMgr.GetInstance().AddNGCount(loadStation.socket == SOCKET.SOCKETA ? Station.治具A站.ToString() : Station.治具B站.ToString(), SocketNGType.点亮.ToString(), 1);
                    TestTimeMgr.GetInstance().Function_AddTestTime("数据统计", 0, false);
                    return StationStep.Step_扫码;
                }
            }

            Bitmap bitmap = Function_GetBMP(capBoxSN);
            LogHelper.OnTest_ShowLog(Name, $"点亮成功\n{msg}", LogType.Warn);

            StationStateHelper.OnTest_ShowStep(Name, "开始通知转盘", LogType.Warn);
            return StationStep.Step_通知转盘;
        }

        public StationStep Step_通知(bool bmanual = false)
        {

            LogHelper.OnTest_ShowLog(Name, "开始通知转盘");
            station_table.Function_NotifyTestEnd(Station.上料站, JsonConvert.SerializeObject(this.loadStation));
            Thread.Sleep(100);
            StationStateHelper.OnTest_ShowStep(Name, "等待转盘到位", LogType.Warn);
            return StationStep.Step_等待转盘到位;
        }
        public override void StopDeal()
        {


        }

        public bool Function_气缸复位()
        {
            Function_回退_下降(true);
            return true;
        }


        public bool Function_工站回零()
        {
            bool bResult = true;
            UserDefineFunction.Axis_回零(this.AxisU, "U轴", this);
            UserDefineFunction.Axis_回零(this.AxisTx, "Tx轴", this);
            return bResult;
        }

        #region MES

        public bool Function_进站(string SN, out string msg)
        {
            msg = "";
            return true;

        }
        public static bool Function_出站(int index, string SN, out string msg)
        {
            msg = "";
            return true;


        }

        public static void FuncexitStation赋值(int index, ref List<TestResult> testResult)
        {
            List<TestResult> testResults = new List<TestResult>();

            //TestResult SerialNumber = new TestResult();
            //SerialNumber.Test_Item = UserTest.Result[index].str_外壳SN.Name.ToString();
            //SerialNumber.Value = UserTest.Result[index].str_外壳SN.Value.ToString();
            //SerialNumber.Low_Limit = UserTest.Result[index].str_外壳SN.Min.ToString();
            //SerialNumber.Up_Limit = UserTest.Result[index].str_外壳SN.Max.ToString();
            //SerialNumber.Result = "OK";
            //testResults.Add(SerialNumber);

            //TestResult SocketerNumber = new TestResult();
            //SocketerNumber.Test_Item = UserTest.Result[index].str_治具编号.Name.ToString();
            //SocketerNumber.Value = UserTest.Result[index].str_治具编号.Value.ToString();
            //SocketerNumber.Low_Limit = UserTest.Result[index].str_治具编号.Min.ToString();
            //SocketerNumber.Up_Limit = UserTest.Result[index].str_治具编号.Max.ToString();
            //SocketerNumber.Result = "OK";
            //testResults.Add(SocketerNumber);

            //TestResult StarTime = new TestResult();
            //StarTime.Test_Item = UserTest.Result[index].StarTime.Name.ToString();
            //StarTime.Value = UserTest.Result[index].StarTime.Value.ToString();
            //StarTime.Low_Limit = UserTest.Result[index].StarTime.Min.ToString();
            //StarTime.Up_Limit = UserTest.Result[index].StarTime.Max.ToString();
            //StarTime.Result = "OK";
            //testResults.Add(StarTime);

            //TestResult EndTime = new TestResult();
            //EndTime.Test_Item = UserTest.Result[index].EndTime.Name.ToString();
            //EndTime.Value = UserTest.Result[index].EndTime.Value.ToString();
            //EndTime.Low_Limit = UserTest.Result[index].EndTime.Min.ToString();
            //EndTime.Up_Limit = UserTest.Result[index].EndTime.Max.ToString();
            //EndTime.Result = "OK";
            //testResults.Add(EndTime);

            //TestResult TestTime = new TestResult();
            //TestTime.Test_Item = UserTest.Result[index].TestTime.Name.ToString();
            //TestTime.Value = UserTest.Result[index].TestTime.Value.ToString();
            //TestTime.Low_Limit = UserTest.Result[index].TestTime.Min.ToString();
            //TestTime.Up_Limit = UserTest.Result[index].TestTime.Max.ToString();
            //TestTime.Result = "OK";
            //testResults.Add(TestTime);

            //TestResult Result = new TestResult();
            //Result.Test_Item = UserTest.Result[index].Result.Name.ToString();
            //Result.Value = UserTest.Result[index].Result.Value.ToString();
            //Result.Low_Limit = UserTest.Result[index].Result.Min.ToString();
            //Result.Up_Limit = UserTest.Result[index].Result.Max.ToString();
            //Result.Result = UserTest.Result[index].Result.Value.ToString();
            //testResults.Add(TestTime);









        }


        #endregion


        #region  气缸动作

        //public void Function_顶升_伸出(bool WaitInPos)
        //{
        //    if(trayIndex== TrayIndexEnum.A)
        //    {
        //        Clyliner_左压合气缸.Function_前进(true);
        //        Thread.Sleep(100);
        //        Clyliner_左顶升气缸.Function_前进(true);
        //    }
        //    else
        //    {
        //        Clyliner_右压合气缸.Function_前进(true);
        //        Thread.Sleep(100);
        //        Clyliner_右顶升气缸.Function_前进(true);

        //    }         

        //}

        public void Function_回退_下降(bool WaitInPos)
        {
            //if (trayIndex == TrayIndexEnum.A)
            //{
            //    Clyliner_右顶升气缸.Function_前进(true);
            //    Clyliner_右压合气缸.Function_前进(true);
            //}
            //else
            //{
            //    Clyliner_右顶升气缸.Function_前进(true);
            //    Clyliner_右压合气缸.Function_前进(true);
            //}



        }
        #endregion


        public bool Function_扫码(ref string strSN)
        {
            if (sys.g_AppMode == AppMode.AirRun)
            {
                strSN = "空跑" + DateTime.Now.ToString("yyyyMMddHHmmss");
                return true;
            }
            if (station_Param.屏蔽扫码._strParamVal)
            {
                strSN = DateTime.Now.ToString("yyyyMMddHHmmss");
                return true;
            }
            if (station_Param.键盘模式._strParamVal)
            {

                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                //SNHelper.OnTest_ReflushSN("SN");
                while (true)
                {

                    if (GlobalVariable.g_StationState == StationState.StationStateEmg)
                        return false;
                    if (UserConfig.b_isScan)
                    {
                        UserConfig.b_isScan = false;
                        strSN = UserConfig.str_SN;
                        if (strSN == string.Empty /*|| strSN.Length != Param_System.Instance.SN长度._strParamVal || !strSN.Contains(Param_System.Instance.SN固定字符._strParamVal)*/)
                        {
                            AlarmMgr.GetIntance().WarnWithDlg(AlarmList.产品SN校验错误.GetHashCode(), new string[] { "重试" }, new string[] { $"产品SN校验错误，读取内容=[{strSN}]" });
                            // SNHelper.OnTest_ReflushSN("SN");

                            return false;
                        }
                        else
                        {

                            LogHelper.OnTest_ShowLog(Name, $"读取产品SN=[{strSN}]", LogType.Info);
                            //LogHelper.OnTest_ShowLog(Name, $"等待双按钮启动", LogType.Warn);
                            //StationStateHelper.OnTest_ShowStep(Name, $"{Name} 等待双按钮启动", LogType.Warn);
                            break;

                        }
                    }
                    Thread.Sleep(10);
                    if (stopwatch.ElapsedMilliseconds > station_Param.扫码超时时间._strParamVal)
                    {
                        AlarmMgr.GetIntance().WarnWithDlg(AlarmList.等到扫码超时.GetHashCode(), new string[] { "重试" }, new string[] { $"SN读取超时，读取内容=[{strSN}]，请重试" });
                        stopwatch.Restart();
                    }
                }

            }
            else
            {

                if (扫码枪?.Fucntion_获取SN码(Param_System.Instance.SN长度._strParamVal, ref strSN, station_Param.扫码超时时间._strParamVal) == 0)
                //if (扫码枪?.Function_触发扫码(Param_System.Instance.SN长度._strParamVal, ref strSN, station_Param.扫码超时时间._strParamVal))
                {
                    strSN = strSN.Replace("\r", "");

                    if (strSN.Contains(Param_System.Instance.SN固定字符._strParamVal))
                    {

                        SNHelper.OnTest_ShowSN("SN", strSN);
                        LogHelper.OnTest_ShowLog(Name, $"读取产品SN=[{strSN}]", LogType.Info);

                        StationStateHelper.OnTest_ShowStep(Name, $"{Name} 等待双按钮启动", LogType.Warn);
                        return true;
                    }
                    else
                    {
                        AlarmMgr.GetIntance().WarnWithDlg(AlarmList.产品SN校验错误.GetHashCode(), new string[] { "重试" }, new string[] { $"产品SN校验错误，读取内容=[{strSN}]" });
                        return false;
                    }

                }
                else
                {
                    AlarmMgr.GetIntance().WarnWithDlg(AlarmList.等到扫码超时.GetHashCode(), new string[] { "重试" }, new string[] { $"SN读取超时，读取内容=[{strSN}]，请重试" });
                    return false;
                }

            }
            return true;

        }

        public bool Function_CapImg(string sn, bool isStop, out string msg)
        {
            if (!isStop)
            {
                string playPath = $"{ParamSetMgr.GetInstance().CurrentWorkDir}\\{ParamSetMgr.GetInstance().CurrentProductFile}\\";
                bool init = ModuleCaptureMgr.Instance.Init(playPath, station_Param.出图图像宽._strParamVal, station_Param.出图图像高._strParamVal);
                if (!init)
                {
                    msg = "初始化出图库失败";
                    return false;
                }

                bool play = ModuleCaptureMgr.Instance.Play(sn == station_Config.str_StationACapBoxSN ? 0 : 1);
                if (!play)
                {
                    msg = "play error";
                    return false;
                }
                msg = "OK";
                return true;
            }
            else
            {
                ModuleCaptureMgr.Instance.Stop(sn == station_Config.str_StationACapBoxSN ? 0 : 1);
                msg = "OK";
                return true;
            }
        }

        public Bitmap Function_GetBMP(string sn)
        {
            int fram = 0;
            Bitmap bitmap = null;
            bool v = ModuleCaptureMgr.Instance.CaptureToBmpRGB(sn == station_Config.str_StationACapBoxSN ? 0 : 1, fram, ref bitmap);

            if (v)
                return bitmap;
            else
                return null;
        }


        #endregion

    }
}
